package com.zw.cloud.tools.utils;


import com.alibaba.fastjson2.JSON;
import org.devlive.sdk.openai.OpenAiClient;
import org.devlive.sdk.openai.entity.ChatEntity;
import org.devlive.sdk.openai.entity.MessageEntity;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

/**
 * 调用OpenAi的49中方法
 */
public class OpenAiUtils {

    private static volatile OpenAiClient client;

    static List<MessageEntity> messages = new CopyOnWriteArrayList<>();

    private static OpenAiClient getOpenAiClient() {
        if (Objects.isNull(client)) {
            synchronized (OpenAiUtils.class) {
                if (Objects.isNull(client)) {
                    client = OpenAiClient.builder()
                            //.apiHost(System.getProperty("proxy.host"))
                            .apiKey("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")
                            .timeout(30)
                            .unit(TimeUnit.SECONDS)
                            .build();
                }
            }
        }
        return client;
    }
    /**
     * 获取ai
     */
    public static List<MessageEntity> getAiResult(String content) {
        OpenAiClient client = getOpenAiClient();

        messages.add(MessageEntity.builder()
                .content(content)
                .build());

        ChatEntity configure = ChatEntity.builder()
                .messages(messages)
                .build();

        client.createChatCompletion(configure)
                .getChoices()
                .forEach(choice -> messages.add(choice.getMessage()));

       /* messages.add(MessageEntity.builder()
                .content("What is my name?")
                .build());

        client.createChatCompletion(configure)
                .getChoices()
                .forEach(choice -> {
                    System.out.println(choice.getMessage());
                });*/

       /* OpenAiClient client = OpenAiClient.builder()
                //.apiHost(System.getProperty("proxy.host"))
                .apiKey(System.getProperty("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"))
                .build();

        CompletionEntity configure = CompletionEntity.builder()
                .model(CompletionModel.TEXT_DAVINCI_003)
                .prompt("测试一下")
                .temperature(2D)
                .build();
        client.createCompletion(configure)
                .getChoices()
                .forEach(System.out::println);*/

        /*OpenAi openAi = new OpenAi();
        if (text) {
            openAi.setModel("text-davinci-003");
        } else {
            openAi.setModel("code-davinci-002");
        }
        openAi.setPrompt(prompt);
        // 0.0 0.1
        openAi.setTemperature(0.0D);
        openAi.setTopP(1D);
        openAi.setPresencePenalty(0D);
        openAi.setFrequencyPenalty(0D);
        //openAi.setStop(Lists.newArrayList("#",";"));
        String OPENAPI_TOKEN = "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";
        Integer TIMEOUT = 60000;
        OpenAiService service = new OpenAiService(OPENAPI_TOKEN, TIMEOUT);
        CompletionRequest.CompletionRequestBuilder builder = CompletionRequest.builder()
                .model(openAi.getModel())
                .prompt(prompt)
                .temperature(openAi.getTemperature())
                .maxTokens(1000)
                .topP(openAi.getTopP())
                .frequencyPenalty(openAi.getFrequencyPenalty())
                .presencePenalty(openAi.getPresencePenalty());
        if (CollectionUtils.isNotEmpty(openAi.getStop())) {
            builder.stop(openAi.getStop());
        }
        CompletionRequest completionRequest = builder.build();
        return service.createCompletion(completionRequest).getChoices();*/
        return messages;
    }

    public static void main(String[] args) {
        List<MessageEntity> result = getAiResult("分析一下数据,找出规律:01,03,05,22,33,55,77,89,90");
        System.out.println(JSON.toJSONString(result));
        List<MessageEntity> result2 = getAiResult("使用java代码实现");
        System.out.println(JSON.toJSONString(result2));

    }
}

